﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Galoap
{
    partial class StringAnswer : Answer
    {

        public StringAnswer() : base() { }

        /// <summary>
        /// method which creates a string representation of the answer
        /// </summary>
        /// <returns>the string representing the answer</returns>
        public override string ToString()
        {
            return this.Data;
        }

        /// <summary>
        /// method which compares data of two different questions
        /// </summary>
        /// <param name="a">the answer that will be compared with this</param>
        /// <returns>true if the data is the same data, false otherwise</returns>
        public override bool CompareData(Answer a)
        {
            // verify if the type is a StringAnswer and if this is equals than param
            if (a is StringAnswer) return this.Data.Equals(((StringAnswer)a).Data);
            else return false;
        }

        /// <summary>
        /// init method which initialize an Answer
        /// </summary>
        /// <param name="data">the string which indicate the data of the answer</param>
        protected override void Initialize(string data)
        {
            // init the data of the answer
            Data = data; 
        }

        /// <summary>
        /// methods which serialize an object in a string
        /// </summary>
        /// <param name="o">the object to serialize</param>
        public override void Serialize(object o)
        {
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

            bf.Serialize(stream, o);

            byte[] b = stream.GetBuffer();
            stream.Close();

            Initialize(System.Convert.ToBase64String(b)); 
        }

        /// <summary>
        /// This method converts the object serialized in the corrispective list of ontologies.
        /// This should be used for post elaboration of the knowledge collected by this games.
        /// </summary>
        /// <returns>the object saved in the serialized answer s</returns>
        public override object Deserialize()
        {
            byte[] bytes = System.Convert.FromBase64String(Data);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes, 0, bytes.Length);

            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            object o = bf.Deserialize(stream);
            stream.Close();
            return o;
        }

    }
}
